package com.ai.dmc.utils.export.impl;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.Validate;

import com.ai.dmc.utils.entity.exportformExhibition.TblCell;
import com.ai.dmc.utils.export.ExportSvcRpt;
import com.ai.dmc.utils.zip.ZipUtil;


/**
 * 
 * @see
 * @author tod
 * @date 2015-2-12 下午2:55:02
 * @version
 * @desc 
 */
public class TxtExportSvcRptImpl implements ExportSvcRpt {

	private FileWriter writer;
	
	private String filePath;
	
	public TxtExportSvcRptImpl(String path) {
		filePath = path.replace("\\\\", "\\").replace("\\", "/");
		int index = filePath.lastIndexOf("/");
		String fileDir = filePath.substring(0,index);
		String form = filePath.substring(filePath.lastIndexOf("."));
		Validate.isTrue(".txt".equalsIgnoreCase(form),"输入的文件路径后缀必须为.txt");
		File dirFile = new File(fileDir);
		if(!dirFile.exists()){
			dirFile.mkdirs();
		}
		try {
			File file = new File(filePath);
			if (!file.exists()) {
				file.createNewFile();
			}
			writer = new FileWriter(file);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("txt初始化服务异常");
		}
	}
	
	
	public void writeData(Object[] objs, Integer index) {
		StringBuffer sb = new StringBuffer("\r\n");
		for (int i = 0; i < objs.length; i++) {
			String value = String.valueOf(objs[i]);
			sb.append(value).append("\t");
		}
		try {
			writer.write(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("Txt服务--写入数据异常");
		}
	}

	public void writeData(List<Object[]> dataSet, Integer index) {
		for (int i = 0; i < dataSet.size(); i++) {
			writeData(dataSet.get(i), index);
		}
	}

	public void writeTblHead(List<TblCell>[] heads, Integer index) {
		StringBuilder sb = new StringBuilder();
		for(int j=0;j<heads.length;j++){
			List<String> headrs = getHeader(heads,j+1);
			for (int i = 0; i < headrs.size(); i++) {
				sb.append(headrs.get(i)).append("\t");
			}
			if(j!=heads.length-1)sb.append("\r\n");
		}
		try {
			writer.write(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("TXT服务写入表头--异常");
		}

	}

	public void writeFile(boolean needZip,String waterMarkName) {
		try {
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(needZip){
			String zipPath = filePath.substring(0,filePath.lastIndexOf(".")) +".zip";
			ZipUtil.compress(filePath, zipPath);
		}
	}

	/**
	 * 获取表头�?��粒度的列名称
	 * 
	 * @param heads
	 * @return
	 */
	private List<String> getHeader(List<TblCell>[] heads,int length){
		List<String> headList = new ArrayList<String>();
		int level = heads.length;
		for(int j=0; j<level;j++){
			for (int i = 0; i < heads[j].size(); i++) {
				TblCell cell = heads[j].get(i);
				int rowSpan = cell.getRowspan();
				int colSpan = cell.getColspan();
				if(j+rowSpan == length || j+1 == length){
					for(int k=0;k<colSpan;k++){
						headList.add((String)cell.getAlias());
					}
				}
			}
		}
		return headList;
	}
}
